package com.datang.pet.api.talkController;/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-06-12 09:32
 * @notify
 * @version 1.0
 */

import com.datang.pet.common.response.Result;
import com.datang.pet.common.utils.StringUtils;
import com.datang.pet.service.TalkService;
import com.datang.pet.vo.AddTalkRequest;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Controller
public class AddTalkController {

    @Autowired
    private TalkService talkService;

    @Autowired
    private MergeProducer mergeProducer;

    /*
     * @author 顶风少年
     * @date 2019/6/13 8:54
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param addTalkRequest
     * @result Result
     * @notify 添加喵文
     * version 1.0
     */
    @PostMapping("/addTalk")
    public Result addTalk(@RequestBody AddTalkRequest addTalkRequest) throws Exception {

        if (!StringUtils.isNUll(addTalkRequest.getUserId())) {
            throw new Exception("用户不能为空");
        }
        if (!StringUtils.isNUll(addTalkRequest.getTalkDescribe())) {
            throw new Exception("不能没有简短描述");
        }
        if (addTalkRequest.getResourceList().size()==0) {
            throw new Exception("不能没有资源");
        }
        String talkType = addTalkRequest.getTalkType();
        if (!StringUtils.isNUll(talkType)) {
            throw new Exception("不能喵文类型");
        }

        if(talkType.equals("0")||talkType.equals("1")){
            //直接添加
            talkService.addTalk(addTalkRequest);
        }else{
            //创建一个消费者监听器
            ActiveMQQueue destination = new ActiveMQQueue("Mp4MergeMp3");
            //将需要处理的请求 存储到生产者队列中
            mergeProducer.sendMessage(destination, addTalkRequest);
        }
        return Result.ok("添加成功");
    }
}
